package org.ss.psci.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.ss.psci.entity.PmAssignment;
import org.ss.psci.util.ListRange;

public class TaskService {

	private BaseDAO baseDAO;
	private List list;

	public static void main(String[] a) {
		System.out.println(new TaskService().getAssignmentId("1", true));
	}

	public String getAssignmentId(String id, boolean flag) {
		if (id == null) {
			return "1";
		}
		if ("".equals(id)) {
			return "1";
		}
		String assignmentId = "";
		int t = 0;
		String[] ids = id.split(".");
		int size = ids.length;
		switch (size) {
		case 0:
			return "";
		case 1: {
			if (flag) {
				t = toInt(ids[0]) - 1;
			} else {
				t = toInt(ids[0]) + 1;
			}
			return toString(t);
		}
		case 2:

		}
		return assignmentId;
	}

	private int toInt(String id) {
		return Integer.parseInt(id);
	}
	private String toString(int id) {
		return String.valueOf(id);
	}

	public List getPmAssignments(String projectsId, String consId) {
		if (projectsId != null && consId != null) {
			if ((!"".equals(projectsId)) && (!"".equals(consId))) {
				return baseDAO.getPmAssignmentDAO().findByProjectIDAndConsId(
						projectsId, consId);
			}
		}
		if (projectsId != null) {
			if (!"".equals(projectsId)) {
				return baseDAO.getPmAssignmentDAO().findByProjectId(projectsId);
			}
		}
		if (consId != null) {
			if (!"".equals(consId)) {
				return baseDAO.getPmAssignmentDAO().findByConsID(consId);
			}
		}

		return new ArrayList();
	}

	public ListRange getPmAssignments(Map condition) {
		int rows, start, limit;
		try {
			try {
				start = Integer.parseInt(condition.get("start").toString());
				limit = Integer.parseInt(condition.get("limit").toString());
			} catch (Exception ex) {
				start = 0;
				limit = 10;
			}
			if ((list == null) || (start == 0)) {
				list = baseDAO.getPmAssignmentDAO().findAll();
			}
			if (list == null) {
				ListRange listRange = new ListRange();
				return listRange;
			} else {
				rows = start + limit > list.size() ? list.size() : start
						+ limit;
			}
			List result = new ArrayList();
			for (int i = start; i < rows; i++) {
				result.add(list.get(i));
			}
			ListRange listRange = new ListRange();
			listRange.setData(result.toArray());
			listRange.setTotalSize(list.size());

			return listRange;
		} catch (Exception ex) {
			System.out.println(ex.toString());
			ListRange listRange = new ListRange();
			return listRange;
		}
	}

	public void savePmAssignment(String temp, Map formMap) {
		PmAssignment pu = new PmAssignment();
		try {
			BeanUtils.populate(pu, formMap);
			PmAssignment p = getPmAssignment(temp);

			if (p != null) {
				p = baseDAO.getPmAssignmentDAO().copyEntity(p, pu);
				baseDAO.getPmAssignmentDAO().save(p);
			} else {
				baseDAO.getPmAssignmentDAO().save(pu);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void addPmAssignment(Map formMap) {
		PmAssignment pu = new PmAssignment();
		try {
			BeanUtils.populate(pu, formMap);

			baseDAO.getPmAssignmentDAO().save(pu);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void delPmAssignment(String temp) {
		try {
			PmAssignment p = getPmAssignment(temp);
			if (p != null) {
				baseDAO.getPmAssignmentDAO().delete(p);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public PmAssignment getPmAssignment(String temp) {
		PmAssignment p = new PmAssignment();
		List uList = baseDAO.getPmAssignmentDAO().findByName(temp);
		if (uList != null) {
			if (uList.size() != 0) {
				p = (PmAssignment) uList.get(0);
			}
		}
		return p;
	}

	public BaseDAO getBaseDAO() {
		return baseDAO;
	}

	public void setBaseDAO(BaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}

}
